<template>
  <static-content-wrapper
    :designer="designer"
    :settings="settings"
    :field="field"
    :design-state="designState"
    :hidden="field.options.hidden"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
  >
    <div class="field-editor switch-field-editor">
      <el-switch
        ref="fieldEditor"
        v-model="fieldModel"
        :active-text="field.options.selectLeftText"
        :inactive-text="field.options.selectRightText"
        :width="scaleWidth"
        :style="{
          height: scaleHeight + 'px'
        }"
        inline-prompt
      >
      </el-switch>
    </div>
  </static-content-wrapper>
</template>

<script>
import emitter from '@/mixins/emitter'
import fieldMixin from '@/mixins/fieldMixin'

export default {
  name: 'SwitchWidget',
  componentName: 'FieldWidget',
  // 必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    settings: Object,
    scale: {
      type: Number,
      default: 1
    },

    designState: {
      type: Boolean,
      default: false
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: ''
    }
  },
  data() {
    return {
      fieldModel: 1
    }
  },
  computed: {
    scaleWidth() {
      return this.field.options.width * this.scale
    },
    scaleHeight() {
      return this.field.options.height * this.scale
    },
    scaleFontSize() {
      return this.field.options.fontSize * this.scale
    },
    fontColor() {
      return this.fieldModel
        ? this.field.options.colorSelectLeftFont
        : this.field.options.colorSelectRightFont
    },
    bgColor() {
      return this.fieldModel
        ? this.field.options.colorSelectLeft
        : this.field.options.colorSelectRight
    },
    lineHeight() {
      return this.scaleHeight + 'px'
    },
    lineRadius() {
      return this.scaleHeight / 2 + 'px'
    },
    cicleHeight() {
      return this.scaleHeight - 4 + 'px'
    },
    cicleRadius() {
      return (this.scaleHeight - 4) / 2 + 'px'
    },
    cicleBgColor() {
      return this.field.options.colorSwitch
    },
    textFontSize() {
      return this.scaleFontSize + 'px'
    }
  },
  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */
    this.registerToRefList()
    this.initEventHandler()

    this.handleOnCreated()
  },

  mounted() {
    this.handleOnMounted()
  },

  beforeUnmount() {
    this.unregisterFromRefList()
  },

  methods: {}
}
</script>

<style lang="scss" scoped>
.switch-field-editor :deep(.el-switch__core) {
  height: v-bind('lineHeight');
  border-radius: v-bind('lineRadius');
  line-height: v-bind('lineHeight');
  background-color: v-bind('bgColor');

  .el-switch__inner {
    height: v-bind('lineHeight');

    .is-text {
      height: v-bind('lineHeight');
      list-style: v-bind('lineHeight');
      font-size: v-bind('textFontSize');
      color: v-bind('fontColor');
    }
  }

  .el-switch__action {
    height: v-bind('cicleHeight');
    width: v-bind('cicleHeight');
    border-radius: v-bind('cicleRadius');
    background-color: v-bind('cicleBgColor');
  }
}

.switch-field-editor .el-switch.is-checked :deep(.el-switch__core) {
  .el-switch__action {
    left: calc(100% - v-bind('cicleHeight') - 1px);
  }
}
</style>
